package com.dykj.fireFighting.web.base.news;

import java.util.*;

import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.news.dto.RealTimeInfoDto;
import com.dykj.fireFighting.api.base.news.entity.RealTimeInfo;
import com.dykj.fireFighting.api.base.news.service.IRealTimeInfoService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * 消防资讯接口
 *
 * @Author JacksonTu
 * @Date  2021-10-19 15:22:57
 */
@Slf4j
@Api(value = "消防资讯接口", tags = {"消防资讯接口"})
@RestController
@RequestMapping("realTimeInfo")
public class RealTimeInfoController {

    @DubboReference
    private IRealTimeInfoService realTimeInfoService;
    @DubboReference
    private IEnterpriseService enterpriseService;


    /**
     * 消防资讯列表
     *
     * @return
     */
    @AutoLog(value = "消防资讯-分页列表查询")
    @ApiOperation(value = "消防资讯分页列表", notes = "消防资讯分页列表")
    @GetMapping("/list")
    //@RequiresPermissions("realTimeInfo/list")
    public CommonResult<PageVo<RealTimeInfo>> treeGrid(RealTimeInfoDto realTimeInfoDto) {
        LoginUserVo user = ShiroUtil.getUser();
        //单位类型(社会单位:0，维保单位:1，监控中心:2)
        Integer enterpriseType = user.getEnterpriseType();
        if(2 == enterpriseType){
            realTimeInfoDto.setUnitId(user.getEnterpriseId().toString());
        }
        if(0 == enterpriseType){
            Enterprise centerEnter = enterpriseService.getInfoByBindingCode(user.getBindingCode());
            realTimeInfoDto.setUnitId(centerEnter.getId().toString());
        }
        PageVo<RealTimeInfo> page = realTimeInfoService.pageList(realTimeInfoDto);
        return CommonResult.success(page);
    }

    /**
     * 获取消防资讯信息
     */
    @AutoLog(value = "消防资讯-获取消防资讯信息")
    @ApiOperation(value = "获取消防资讯信息", notes = "获取消防资讯信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    //@RequiresPermissions("realTimeInfo/info")
    public CommonResult<RealTimeInfo> info(@PathVariable("id") String id){
        RealTimeInfo realTimeInfo = realTimeInfoService.getById(id);
        return CommonResult.success(realTimeInfo);
    }

    /**
     * 保存消防资讯信息
     */
    @AutoLog(value = "消防资讯-保存消防资讯信息")
    @ApiOperation(value = "保存消防资讯信息", notes = "保存消防资讯信息")
    @PostMapping("/save")
    //@RequiresPermissions("realTimeInfo/save")
    public CommonResult save(@Valid @RequestBody RealTimeInfo realTimeInfo) {
        try {
            LoginUserVo user = ShiroUtil.getUser();

            realTimeInfo.setCreateTime(new Date());
            realTimeInfo.setUintId(user.getEnterpriseId().toString());
            realTimeInfo.setUnitName(user.getEnterpriseName());
            realTimeInfo.setCreateUserId(user.getId().toString());
            realTimeInfo.setCreateUserName(user.getLoginName());
            realTimeInfo.setIsDel(0);
                realTimeInfoService.save(realTimeInfo);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改消防资讯信息
     */
    @AutoLog(value = "消防资讯-修改消防资讯信息")
    @ApiOperation(value = "修改消防资讯信息", notes = "修改消防资讯信息")
    @PostMapping("/update")
    //@RequiresPermissions("realTimeInfo/update")
    public CommonResult update(@Valid @RequestBody RealTimeInfo realTimeInfo) {
        try {
            LoginUserVo user = ShiroUtil.getUser();
            realTimeInfo.setUpdateTime(new Date());
            realTimeInfo.setUpdateUserId(user.getId().toString());
            realTimeInfo.setUpdateUserName(user.getLoginName());
                realTimeInfoService.updateById(realTimeInfo);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除消防资讯信息
     */
    @AutoLog(value = "消防资讯-删除消防资讯信息")
    @ApiOperation(value = "删除消防资讯信息", notes = "删除消防资讯信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    //@RequiresPermissions("realTimeInfo/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                realTimeInfoService.updateDel(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
